package cn.rkylin.oms.salespromotion.rules.util.handler.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.order.vo.OrderVO;
import cn.rkylin.oms.salespromotion.rules.util.handler.RuleMatchHandler;
import cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO;

/**
 * 类名: EachDayCheckHandler <br/>
 * 方法名: 校验订单时间是否符合每天时间要求 <br/>
 * date: 2017年11月20日 下午4:01:57 <br/>
 *
 * @author zhanghao
 * @version 
 */
public class EachDayCheckHandler extends RuleMatchHandler {

    /**
     * @see cn.rkylin.oms.salespromotion.rules.util.handler.RuleMatchHandler#getValidMap(cn.rkylin.oms.order.vo.OrderVO, cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO)
     */
    @Override
    public Map<String, String> getValidMap(OrderVO orderVO, SalesPromotionRuleVO ruleVO) {
        String timeRange = "["+ruleVO.getEachDayTimeStart()+"-"+ruleVO.getEachDayTimeEnd()+"]";
        Map<String, String> result = new HashMap<String, String>();
        result.put(RESULT, SUCCESS);
        if(YesNoConst.YES.equals(ruleVO.getIsEachdaySetEnable())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = "";
            if(ruleVO.COUNTING_DATE_TYPE_ORDER_TIME==ruleVO.getCountingDateType()) {
                // 如果是拍单时间，则校验拍单时间是否在每日时间区间内
                time = df.format(orderVO.getOrderTime()).substring(11);
            }else {
                // 如果是付款时间，则校验付款时间是否在每日时间区间内
                time = df.format(orderVO.getPaidDate()).substring(11);
            }
            if(!isInTime(timeRange,time )) {
                result.put(RESULT, "拍单时间不在每天的时间区间内，将跳过规则["+ruleVO.getRuleName()+"]");
            }
        }
        return result;
    }

    /**
     * 判断某一时间是否在一个区间内
     * 
     * @param sourceTime
     *            时间区间,半闭合,如[10:00-20:00)
     * @param curTime
     *            需要判断的时间 如10:00
     * @return 
     * @throws IllegalArgumentException
     */
    private boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            } 
            else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }

    }

}
